﻿<Window x:Class="Domino.GameBoard"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:domino="clr-namespace:Domino"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:media="clr-namespace:System.Windows.Media;assembly=PresentationCore"
        Title="{Binding AppTitle}" WindowState="Maximized" Height="350" Width="525">
    <Window.Resources>
        <domino:CompressBoardConverter x:Key="CompressBoardConverter" />
        <domino:VisibilityConverter x:Key="VisibilityConverter" />
        <domino:CurrentPlayerConverter x:Key="CurrentPlayerConverter" />
        <domino:InversionConverter x:Key="InversionConverter" />

        <sys:Boolean x:Key="True">True</sys:Boolean>
        <sys:Boolean x:Key="False">False</sys:Boolean>

        <ObjectDataProvider x:Key="GameTypes" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="domino:eGameType"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="TileOrientations" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="domino:eTileOrientation"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <Style TargetType="Label">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
        </Style>
    </Window.Resources>

    <Grid Name="MainLayout">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!--<StackPanel Grid.ColumnSpan="2" Orientation="Horizontal">-->
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Select game type: " />
            <ComboBox Name="cmbGameTypes" ItemsSource="{Binding Source={StaticResource GameTypes}}" SelectedItem="{Binding SelectedGameType, Mode=TwoWay}" />
        </StackPanel>

        <StackPanel Name="stkSettings" Grid.ColumnSpan="2" HorizontalAlignment="Right" Orientation="Horizontal" Panel.ZIndex="{x:Static sys:Int32.MaxValue}">
            <StackPanel Name="stkTileOrientations" Orientation="Horizontal" 
                        Visibility="{Binding ElementName=btnSettings, Path=IsChecked, Converter={StaticResource VisibilityConverter}}">
                <CheckBox Name="chkHint" IsChecked="{Binding AllowHint, Mode=TwoWay}" />
                <TextBlock FontWeight="Bold" Text="Tile orientations | " />
                <TextBlock Text="Boneyard: " />
                <ComboBox ItemsSource="{Binding Source={StaticResource TileOrientations}}" SelectedItem="{Binding BoneyardOrientation, Mode=TwoWay}" />
                <TextBlock Text="Player: " />
                <ComboBox ItemsSource="{Binding Source={StaticResource TileOrientations}}" SelectedItem="{Binding PlayerOrientation, Mode=TwoWay}" />
                <TextBlock Text="Board: " />
                <ComboBox ItemsSource="{Binding Source={StaticResource TileOrientations}}" SelectedItem="{Binding BoardOrientation, Mode=TwoWay}" />
                <TextBlock Text="Board (pair): " />
                <ComboBox ItemsSource="{Binding Source={StaticResource TileOrientations}}" SelectedItem="{Binding BoardOrientationPair, Mode=TwoWay}" />
            </StackPanel>
            <ToggleButton Name="btnSettings" Grid.Column="1" HorizontalAlignment="Right" 
                      Height="25" Width="{Binding ElementName=btnSettings, Path=ActualHeight}">
                <Image Name="imgSettings" Stretch="UniformToFill" Source="/Domino;component/Resources/settings.png" />
            </ToggleButton>
        </StackPanel>

        <StackPanel Name="stkPlayers" Grid.Row="1" Orientation="Horizontal">
            <TextBlock Text="Select no. of players: " />
            <ComboBox Name="cmbPlayerCount" ItemsSource="{Binding AllowedPlayerCount}" SelectedItem="{Binding PlayerCount, Mode=TwoWay}" />

            <ItemsControl Name="imsctrlPlayers" ItemsSource="{Binding PlayerNames}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Width="60" HorizontalAlignment="Right" FontWeight="Bold" Content="{Binding Label}" />
                            <TextBox Height="23" IsEnabled="{Binding IsEnabled}" Text="{Binding Name, Mode=TwoWay}" />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

            <Button Name="btnInitialiseGame" Grid.Row="1" Content="Start/Reset Game" Command="{Binding StartGameCommand}" CommandParameter="{StaticResource True}" />
            <TextBlock Name="txtbStartPlayer" Text="{Binding Model.PlayerRotation.Name, StringFormat='Starting: {0}', FallbackValue='--'}" />
            <Button Name="btnNextRound" Grid.Row="1" Content="Next round" IsEnabled="{Binding Model.AllowNextRound, FallbackValue=False}" Command="{Binding StartGameCommand}" CommandParameter="{StaticResource False}" />
            <Button Name="btnNextMove" Content="Next move" Command="{Binding NextMoveCommand}" IsEnabled="{Binding Model.AllowNextRound, FallbackValue=False, Converter={StaticResource InversionConverter}, ConverterParameter={x:Type sys:Boolean}}" />
        </StackPanel>

        <StackPanel Name="stkBoneyard" Grid.Row="2">
            <TextBlock Name="txtbBoneyard" HorizontalAlignment="Left" IsEnabled="{Binding Model.IsBoneyardEnabled}" 
                       FontWeight="Bold" FontSize="18" Text="Boneyard" />
            <ItemsControl Name="itmctrlBoneyard" IsEnabled="{Binding Model.IsBoneyardEnabled}" ItemsSource="{Binding Model.BoneYard}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Width="{Binding ElementName=MainLayout, Path=ActualWidth}" Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <domino:Tile TilePairValue="{Binding }" 
                                     Command="{Binding DataContext.PlayTileCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" CommandParameter="{Binding }">
                        </domino:Tile>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>

        <ItemsControl Name="itmsctrlPlayer" Grid.Row="3" Grid.ColumnSpan="2" ItemsSource="{Binding Model.PlayerTiles}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="2" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Name="brdPlayer" Margin="1" Padding="2" BorderBrush="LightGray" BorderThickness="1" CornerRadius="5" 
                            HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                            Background="{Binding IsCurrent, Converter={StaticResource CurrentPlayerConverter}, ConverterParameter={x:Static media:Brushes.LightGreen}}">
                        <Grid Name="grdPlayer" IsEnabled="{Binding IsCurrent}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <StackPanel Orientation="Vertical">
                                <StackPanel Name="stkPlayerName" HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal">
                                    <TextBlock FontWeight="Bold" FontSize="18" Text="{Binding Name}" />
                                    <TextBlock Text=" (current)" Visibility="{Binding IsCurrent, Converter={StaticResource VisibilityConverter}}" />
                                </StackPanel>

                                <UniformGrid Name="uniScore" Grid.Row="1" Columns="1" HorizontalAlignment="Left" VerticalAlignment="Top">
                                    <TextBlock Name="txtbScore" Text="{Binding Score, StringFormat='Score: {0}'}" />
                                    <TextBlock Name="txtbBoardScore" Text="{Binding BoardScore, StringFormat='Board score: {0}'}" />
                                    <TextBlock Name="txtbResidualScore" Text="{Binding ResidualScore, StringFormat='Residual score: {0}'}" />
                                </UniformGrid>

                                <Button Name="btnPass" Width="Auto" Content="P A S S" IsEnabled="{Binding IsPassEnabled}" Command="{Binding DataContext.Model.PassCommand}" />
                            </StackPanel>

                            <ItemsControl Name="itmctrlTiles" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding Tiles}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <domino:Tile x:Name="tlPlayerTile" TilePairValue="{Binding}" 
                                                     Command="{Binding DataContext.PlayTileCommand, RelativeSource={RelativeSource AncestorType=ItemsControl, AncestorLevel=2}}" CommandParameter="{Binding }"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <StackPanel Name="stkBoard" Grid.Row="4" Grid.ColumnSpan="2" Orientation="Vertical">
            <TextBlock FontWeight="Bold" Text="{Binding Model.CompressBoardText, StringFormat='Compressed : {0}', TargetNullValue='Compressed : --', FallbackValue='Compressed : --'}" />
            <TextBlock FontWeight="Bold" Text="{Binding Model.BoardText, StringFormat='Full Board : {0}', TargetNullValue='Full Board : --', FallbackValue='Full Board : --'}" />
            <TextBlock FontWeight="Bold" Text="{Binding Model.BoardValue, StringFormat='Board: {0}', TargetNullValue='Board: --', FallbackValue='Board: --'}" />
            <Border Name="brdBoard" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                    BorderThickness="2" BorderBrush="Black">
                <ItemsControl Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" 
                              ItemsSource="{Binding Model.Board, Converter={StaticResource CompressBoardConverter}, ConverterParameter=3}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <domino:Tile TilePairValue="{Binding }" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Border>
        </StackPanel>
    </Grid>
</Window>
